package com.lookme.lmtool.cracker;

import cn.hutool.core.lang.ClassScanner;
import com.lookme.lmtool.cracker.filter.AbstractFilter;
import com.lookme.lmtool.cracker.filter.IFilter;
import com.lookme.lmtool.cracker.spliter.AbstractSpliter;
import com.lookme.lmtool.cracker.spliter.ISpliter;

import java.util.*;

/**
 * 图像过滤器与图像分割器加载工厂
 *
 * @author blake
 */
public class CrackerFactory {
    /**
     * 目前系统支持的图像过滤器
     */
    public static List<IFilter> SUPPORT_FILTER_LIST;
    /**
     * 目录系统支持的图像分割器
     */
    public static List<ISpliter> SUPPORT_SPLITER_LIST;

    static {
        SUPPORT_SPLITER_LIST = new ArrayList<>();

        Set<Class<?>> classes = ClassScanner.scanPackageBySuper(AbstractSpliter.class.getPackage().getName(), AbstractSpliter.class);
        classes.forEach(aClass -> {
            try {
                SUPPORT_SPLITER_LIST.add((ISpliter) aClass.newInstance());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        Collections.sort(SUPPORT_SPLITER_LIST, new Comparator<ISpliter>() {
            @Override
            public int compare(ISpliter o1, ISpliter o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });

        SUPPORT_FILTER_LIST = new ArrayList<>();

        classes = ClassScanner.scanPackageBySuper(AbstractFilter.class.getPackage().getName(), AbstractFilter.class);
        classes.forEach(aClass -> {
            try {
                SUPPORT_FILTER_LIST.add((IFilter) aClass.newInstance());
            } catch (Exception e) {

            }
        });

        Collections.sort(SUPPORT_FILTER_LIST, new Comparator<IFilter>() {
            @Override
            public int compare(IFilter o1, IFilter o2) {
                return (o1.getGroupName() + o1.getName()).compareTo((o2.getGroupName() + o2.getName()));
            }
        });

    }
}
